package  com.makinolo.rlvforge.tacx.file
{
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.utils.ByteArray;
import flash.utils.Endian;

import com.makinolo.rlvforge.tacx.data.Header;
import com.makinolo.rlvforge.tacx.data.InfoBlock;
import com.makinolo.rlvforge.tacx.utils.CRC32;
import com.makinolo.rlvforge.tacx.data.GeneralInfo;

public class CatalystProgram
{	
	// A catalyst program file consists of a Header, at last one info block 
	// and the program data	
	private var m_head:Header;
	private var m_ginfo:GeneralInfo ;
	private var m_pdata:Array; //ProgramData[]  
	
	/**
	 * This constructor reads a pgmf file and fills the program data with it's contents
	 * 
	 * @param file
	 * @throws Exception 
	 */
/*	public CatalystProgram(InputStream fis) throws Exception
	{				
		try
		{												
			LEDataInputStream dis=new LEDataInputStream(fis);
			
			m_head = new Header(dis);
			if (m_head.getFileFingerprint() != Header.PGMF_FINGERPRINT)
			{
				throw new Exception("Wrong Tacx file type "+m_head.getFileFingerprint());
			}
			else
			{
				InfoBlock iblock;
				for (int i=0; i<m_head.getBlockCount(); i++)
				{
					iblock = new InfoBlock(dis);
					if (iblock.getBlockFingerprint() == InfoBlock.GENERAL_INFO)
					{
						// There should be only one, if there are more get the last
						for (int j=0; j<iblock.getRecordCount(); j++)	
							m_ginfo = new GeneralInfo(dis);
					}
					else if (iblock.getBlockFingerprint() == InfoBlock.PROGRAM_DETAILS)
					{
						m_pdata = new ProgramData[iblock.getRecordCount()];
						for (int j=0; j<iblock.getRecordCount(); j++)
							m_pdata[j] = new ProgramData(dis);							
					}
					
				}
			}
		}		
		catch (IOException e)
		{
			throw e;
		}					
	}
*/
	public function CatalystProgram()
	{
		m_head = new Header(Header.PGMF_FINGERPRINT, 100, 2)				
	}
	
	public function getGeneralInfo():GeneralInfo
	{
		return m_ginfo;
	}

	public function setGeneralInfo(m_ginfo:GeneralInfo):void
	{
		this.m_ginfo = m_ginfo;
	}

	public function getHeader():Header
	{
		return m_head;
	}

	public function setHeader(m_head:Header):void
	{
		this.m_head = m_head;
	}

	public function getProgramData():Array
	{
		return m_pdata;
	}

	public function setProgramData(m_pdata:Array):void
	{
		this.m_pdata = m_pdata;
		
		// Reading the program data in a byte buffer in order to calculate the
		// crc value		
		var auxdos:ByteArray=new ByteArray();
		for (var i:int=0; i<m_pdata.length; i++)
			m_pdata[i].persist(auxdos);
			
		m_ginfo.setCheckSum(CRC32.Calculate(auxdos));
	}
	
	public function toFile(file:File):void
	{		
		var dos:ByteArray=new ByteArray();
		dos.endian = Endian.LITTLE_ENDIAN;
		// First the header is written
		m_head.persist(dos);				
		// Now create an infoblock for General info
		var ib:InfoBlock  = new InfoBlock(InfoBlock.GENERAL_INFO, 100, 1, GeneralInfo.RECORD_SIZE);
		ib.persist(dos);
		m_ginfo.persist(dos);		
		ib = new InfoBlock(InfoBlock.PROGRAM_DETAILS, 100, m_pdata.length, com.makinolo.rlvforge.tacx.data.ProgramData.RECORD_SIZE);
		ib.persist(dos);
		
		for (var i:int=0; i<m_pdata.length; i++)
			m_pdata[i].persist(dos);		
			
		var fs:FileStream = new FileStream();
		fs.open(file, FileMode.WRITE);
		fs.writeBytes(dos,0, dos.length);		
		fs.close();			
	}
}
}